﻿@using FormBuilder.Components.Shared
@using System.Text.Json
@using FormBuilder.Models.Url
@inject DialogService dialogService

<RadzenStack>
    <RadzenTabs @bind-SelectedIndex=@selectedIndex>
        <Tabs>
            <RadzenTabsItem Text="Properties">
                <RadzenStack>
                    <!-- Act as a button -->
                    <div>
                        <RadzenCheckBox @bind-Value="CopyRecord.ActAsButton" />
                        <RadzenLabel>Act as a button ?</RadzenLabel>
                    </div>
                    <!-- Is static link -->
                    <div>
                        <RadzenCheckBox @bind-Value="CopyRecord.IsStaticLink" />
                        <RadzenLabel>Is static link ?</RadzenLabel>
                    </div>
                    @if(CopyRecord.IsStaticLink)
                    {

                        <!-- Url -->
                        <div>
                            <RadzenLabel Text="URL"></RadzenLabel>
                            <RadzenTextBox @bind-Value="@CopyRecord.Url"></RadzenTextBox>
                        </div>
                        <!-- Open in new tab  -->
                        <div>
                            <RadzenCheckBox @bind-Value="CopyRecord.OpenInNewTab" />
                            <RadzenLabel>Open in new tab ?</RadzenLabel>
                        </div>
                    }
                </RadzenStack>
            </RadzenTabsItem>
            <RadzenTabsItem Text="Label">
                <EditLabelsComponent Labels="@CopyRecord.Labels" />
            </RadzenTabsItem>
        </Tabs>
    </RadzenTabs>
    <RadzenButton Text="Save" Click="@HandleSave"></RadzenButton>
</RadzenStack>

@code {
    public int selectedIndex { get; set; } = 0;
    FormAnchorRecord CopyRecord { get; set; } = new FormAnchorRecord();
    [Parameter] public FormAnchorRecord Record { get; set; }
    [Parameter] public WorkflowContext WorkflowContext { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if(Record != null)
        {
            CopyRecord = JsonSerializer.Deserialize<FormAnchorRecord>(JsonSerializer.Serialize(Record));
        }
    }

    private void HandleSave()
    {
        Record.ActAsButton = CopyRecord.ActAsButton;
        Record.Transformations = CopyRecord.Transformations;
        Record.Labels = CopyRecord.Labels;
        Record.IsStaticLink = CopyRecord.IsStaticLink;
        Record.Url = CopyRecord.Url;
        Record.OpenInNewTab = CopyRecord.OpenInNewTab;
        dialogService.Close();
    }
}